#include "date.h"
#include "ui_date.h"

#define COLUMN_SPACING 20
Date::Date(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Date)
{
    SetMainBacklight();

    ui->setupUi(this);
    ycapi = new Ycapi();

    timeTimer=new QTimer(this);
    connect(timeTimer,SIGNAL(timeout()),this,SLOT(timeTimerUpdate()));
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
     timeTimer->start(1000);

     ui->dateTimeEdit->setAttribute(Qt::WA_InputMethodEnabled,false);

   //  QSettings settings ("text.ini",QSettings::IniFormat);
     //settings.sync();

}
void Date::SetMainBacklight()
{
QPalette bgpal = palette();
bgpal.setColor (QPalette::Background, QColor (0, 0 , 0, 255));
bgpal.setColor (QPalette::Foreground, QColor (255,0,0,255));
setPalette (bgpal);
}

Date::~Date()
{
    delete ui;
}

void Date::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Date::timeTimerUpdate()
{
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
}


void Date::on_pushButton_clicked()
{
        char cmd[50]={0};
        timeTimer->stop();

        sprintf(cmd,"date -s  \"%d-%02d-%02d %02d:%02d:%02d\"", ui->dateTimeEdit->date().year(),
                ui->dateTimeEdit->date().month(),
                ui->dateTimeEdit->date().day(),
                ui->dateTimeEdit->time().hour(),
                ui->dateTimeEdit->time().minute(),
                ui->dateTimeEdit->time().second());

        system(cmd);
        system("hwclock -w");
       timeTimer->start(1000);
}



void Date::on_pushButton_2_clicked()
{
      timeTimer->stop();
}

void Date::on_close_clicked()
{
    this->close();
}
